package com.cn.algorithm.leetcode.tree;

/*
*  给定一个二叉树，判断它是否是高度平衡的二叉树。
*
*/
public class Solution1 {

    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        int l = depth(root.left);
        int r = depth(root.right);
        return Math.abs(l - r) <= 1;
    }
    public int depth(TreeNode root){
        if(root==null) return 0;
        return Math.max(depth(root.left),depth(root.right)) + 1;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
